#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <queue>
#include <numeric>

using namespace std;

class Solution {
public:
    vector<int> maxSubsequence(vector<int>& nums, int k) {
        /* 优先队列，获取前k个最大的元素和对应索引 */
        auto cmp = [](pair<int, int> a, pair<int, int> b){
            return a.first > b.first;
        };
        priority_queue<pair<int, int>, std::vector<pair<int, int>>, decltype(cmp)> priQue(cmp);
        for (int idx = 0; idx < nums.size(); idx++) {
            auto pairVal = make_pair(nums[idx], idx);
            priQue.push(pairVal);
            if (priQue.size() > k) {
                priQue.pop();
            }
        }

        /* 按照索引顺序提取元素 */
        vector<pair<int, int>> tmp{};
        while (!priQue.empty()) {
            tmp.push_back(priQue.top());
            priQue.pop();
        }

        sort(tmp.begin(), tmp.end(), [](pair<int, int> a, pair<int, int> b){ return a.second < b.second; });
        vector<int> ret{};
        for (const auto& elem : tmp) {
            ret.push_back(elem.first);
        }

        return ret;
    }
};


int main()
{   
    Solution s;
    vector<int> vt{5, 3,2,4,3};
    vector<int> ret = s.maxSubsequence(vt, 2);

    for (auto num : ret) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}